# coding=utf-8
import xadmin
from xadmin.layout import *
from xadmin.plugins.inline import Inline
from xadmin.plugins.batch import BatchChangeAction
from django.contrib.comments.models import Comment

from models import *


class MaintainInline(object):
    model = MaintainLog
    extra = 0
    style = 'accordion'


class IDCAdmin(object):
    list_display = ('name', 'description', 'create_time')
    list_display_links = ('name')
    wizard_form_list = [
        (u'第一步', ('name', 'description')),
        (u'第二步', ('contact', 'telphone', 'dongdong', 'address')),
        (u'完成', ('customer_id'))
    ]

    search_fields = ['name']
    relfield_style = 'fk-ajax'
    reversion_enable = True

    actions = [BatchChangeAction]
    batch_fields = ('contact')


class CommentInline(object):
    generic_inline = True
    ct_fk_field = "object_pk"
    model = Comment
    extra = 1
    style = 'tab'


class ServerAdmin(object):
    def open_web(self, instance):
        return "<a href='http://%s' target='_blank'>打开</a>" % instance.ip

    open_web.short_description = "管理"
    open_web.allow_tags = True
    open_web.is_column = True

    def thumbnail_img(self, obj):
        return "<img src='/static/server.png'/>"

    thumbnail_img.short_description = u"缩略图"
    thumbnail_img.allow_tags = True
    thumbnail_img.is_column = True
    thumbnail_img.thumbnail_img = True

    list_display = (
        'thumbnail_img', 'name', 'idc', 'guarantee_date', 'service_type', 'status', 'open_web', 'description')
    list_display_links = ('name')
    grid_layouts = ['thumbnails', 'table']

    raw_id_fields = ('idc')
    style_fields = {'system': "radio-inline"}

    search_fields = ['name', 'ip', 'description']
    list_filter = ['idc', 'guarantee_date', 'status', 'brand', 'model', 'cpu', 'core_num', 'hard_disk', 'memory',
                   'service_type']

    list_bookmarks = [{'title': u"报修", 'query': {'status__exact': 2}, 'order': ('-guarantee_date',),
                       'cols': ('brand', 'guarantee_date', 'service_type')}]

    show_detail_fields = ('idc')
    list_editable = ('name', 'idc', 'guarantee_date', 'service_type', 'description')
    save_as = True

    # aggregate_fields = {"guarantee_date": "min"}

    actions = {BatchChangeAction}
    batch_fields = ('name', 'idc', 'guarantee_date', 'service_type', 'status', 'description', 'system')

    form_layout = (
        Main(
            TabHolder(
                Tab('Comm Fiels',
                    Fieldset('Company data',
                             'name', 'idc',
                             description="some comm fields, required"
                             ),
                    Inline(MaintainLog),
                    ),
                Tab('Extend Fiedls',
                    Fieldset('Contact details',
                             'service_type',
                             Row('brand', 'model'),
                             Row('cpu', 'core_num'),
                             Row(AppendedText('hard_disk', 'G'), AppendedText('memory', "G")),
                             'guarantee_date'
                             ),
                    ),
                Tab('Comments',
                    Inline(Comment),
                    ),
            ),
        ),
        Side(
            Fieldset('状态数据',
                     'status', 'ssh_port', 'ip'
                     ),
        )
    )
    inlines = [MaintainInline, CommentInline]
    reversion_enable = True


class MaintainLogAdmin(object):
    list_display = ('operator', 'maintain_type', 'hard_type', 'time', 'operator', 'note')
    list_display_links = ('operator')

    list_filter = ['maintain_type', 'hard_type', 'time', 'operator']
    search_fields = ['note']

    form_layout = (
        Col("col2",
            Fieldset(u'记录数据',
                     'time', 'note',
                     css_class='unsort short_label no_title'
                     ),
            span=9, horizontal=True
            ),
        Col("col1",
            Fieldset('通讯资料',
                     'hbitdb', 'maintain_type'
                     ),
            Fieldset(u'详细信息',
                     'hard_type', 'operator'
                     ),
            span=3
            )
    )
    reversion_enable = True


class AccessRecordAdmin(object):
    def avg_count(self, instance):
        return int(instance.view_count / instance.user_count)

    avg_count.short_description = u"平均计数"
    avg_count.allow_tags = True
    avg_count.is_column = True

    list_display = ('date', 'user_count', 'view_count', 'avg_count')
    list_display_links = ('date')

    list_filter = ['date', 'user_count', 'view_count']
    actions = None
    aggregate_fields = {"user_count": "sum", 'view_count': "sum"}

    refresh_times = (3, 5, 10)
    data_charts = {
        "user_count": {'title': u"用户报告", "x-field": "date", "y-field": ("user_count", "view_count"),
                       "order": ('date',)},
        "avg_count": {'title': u"平均报告", "x-field": "date", "y-field": ('avg_count',), "order": ('date')}
    }


'''
定义交换机
'''


class Switch(object):
    def open_ssh(self, instance):
        return "<a href='telnet://%s' target='_blank'>打开</a>" % instance.ip

    open_ssh.short_description = u"telnet访问"
    open_ssh.allow_tags = True
    open_ssh.is_column = True

    def thumbnail_img(self, obj):
        return "<img src='/static/switch.png'/>"

    thumbnail_img.short_description = u"缩略图"
    thumbnail_img.allow_tags = True
    thumbnail_img.is_column = True
    thumbnail_img.thumbnail_img = True

    list_display = (
        'thumbnail_img', 'quyu', 'city', 'Room', 'weizhi', 'pinpai', 'mingcheng', 'sn', 'xzcode', 'ywcode', 'usetime',
        'ip',
        'beizhu',)
    list_display_links = 'Room'
    search_fields = ['quyu', 'city', 'Room', 'weizhi', 'pinpai', 'mingcheng', 'sn', 'xzcode', 'ywcode', 'usetime', 'ip',
                     'beizhu']

    list_filter = ['quyu', 'city', 'Room', 'weizhi', 'pinpai', 'mingcheng', 'xzcode', 'ywcode', 'usetime']
    actions = None
    aggregate_fields = {'sum': 'quyu', 'sum': 'city', 'sum': 'Room', 'sum': 'weizhi', 'sum': 'pinpai',
                        'sum': 'mingcheng', 'sum': 'xzcode', 'sum': 'ywcode', 'sum': 'usetime'}


class SwitchGroupAdmin(object):
    list_display = ('name', 'description')
    list_display_links = ('name')

    search_fields = ['name']
    style_fields = {'switch': 'checkbox-inline'}


class ServerGroupAdmin(object):
    list_display = ('name', 'description')
    list_display_links = ('name')

    search_fields = ['name']
    style_fields = {'servers': 'checkbox-inline'}


'''
定义防火墙
'''


class Firewall(object):
    list_display = ('Room', 'name', 'SID', 'code', 'zgxip', 'bgxip', 'Roomdata', 'Location', 'beizhu')
    list_display_links = ('name', 'SID', 'code', 'zgxip', 'bgxip', 'Roomdata', 'Location')
    search_fields = ['name']
    style_fields = {'Firewall': 'checkbox-inline'}
    # 定义过滤器
    list_filter = ('Room', 'name', 'SID', 'code', 'zgxip', 'bgxip', 'Roomdata', 'Location')
    actions = None
    aggregate_fields = {'sum': 'Room', 'sum': 'name', 'sum': 'SID', 'sum': 'code', 'sum': 'zgxip', 'sum': 'bgxip',
                        'sum': 'Roomdata', 'sum': 'Location'}


'''
定义UPS
'''


class Ups(object):
    list_display = (
        'syweizhi', 'kufangname', 'xinghao', 'gonglv', 'dcsl', 'aztime', 'SN', 'zccode', 'baoxiu', 'lxr', 'lxfs',
        'lxdz',
        'beizhu')
    list_display_links = ('xinghao', 'lxr')
    search_fields = ['syweizhi', 'kufangname', 'xinghao', 'gonglv', 'dcsl', 'aztime', 'SN', 'zccode', 'baoxiu', 'lxr',
                     'lxfs']
    style_fields = {'Ups': 'checkbox-inline'}
    # 定义ups过滤器
    list_filter = (
        'syweizhi', 'kufangname', 'xinghao', 'gonglv', 'dcsl', 'aztime', 'SN', 'zccode', 'baoxiu', 'lxr', 'lxfs',
        'lxdz',
        'beizhu')
    actions = None
    aggregate_fields = {'sum': 'syweizhi', 'sum': 'kufangname', 'sum': 'xinghao', 'sum': 'aztime', 'sum': 'SN',
                        'sum': 'zccode', 'sum': 'baoxiu', 'sum': 'lxr', 'sum': 'lxfs'}


'''
定义宽带
'''


class Broadband(object):
    list_display = ('kufang', 'xinghao', 'isp', 'wwip', 'gw', 'nwwd', 'daikuan', 'osver', 'beizhu')
    list_display_links = ('kufang', 'xinghao', 'isp', 'wwip', 'gw', 'nwwd', 'daikuan', 'osver', 'beizhu')
    search_fields = ['kufang', 'xinghao', 'isp', 'wwip', 'gw', 'nwwd', 'daikuan', 'osver', 'beizhu']
    style_fields = {'Broadband': 'checkbox-inline'}


'''
定义光纤
'''


class Fiber(object):
    list_display = ('quyu', 'kuqu', 'isp', 'gxhao', 'kehujl', 'phone')
    list_display_links = ('quyu', 'kuqu', 'isp', 'gxhao', 'kehujl', 'phone')
    search_fields = ['quyu', 'kuqu', 'isp', 'gxhao', 'kehujl', 'phone']
    style_fields = {'Fiber': 'checkbox-inline'}


'''
定义ap
'''


class AP(object):
    list_display = (
        'szdd', 'xinghao', 'sn', 'gzbh', 'mac', 'lanip', 'vanip', 'loginname', 'loginpasswd', 'wifiname', 'wifipasswd')
    list_display_links = (
        'szdd', 'xinghao', 'sn', 'gzbh', 'mac', 'lanip', 'vanip', 'loginname', 'loginpasswd', 'wifiname', 'wifipasswd')
    search_fields = ['szdd', 'xinghao', 'sn', 'gzbh', 'mac', 'lanip', 'vanip', 'loginname', 'loginpasswd', 'wifiname',
                     'wifipasswd']
    style_fields = {'AP': 'checkbox-inline'}


class AC(object):
    list_display = ('quyu', 'cangbie', 'zhuangtai', 'ac', 'ap', 'apxh', 'beizhu')
    list_display_links = ('quyu', 'cangbie', 'zhuangtai', 'ac', 'ap', 'apxh', 'beizhu')
    search_fields = ['quyu', 'cangbie', 'zhuangtai', 'ac', 'ap', 'apxh', 'beizhu']
    style_fields = {'AC': 'checkbox-inline'}


xadmin.site.register(IDC, IDCAdmin)
xadmin.site.register(Switchs, Switch)
xadmin.site.register(Server, ServerAdmin)
xadmin.site.register(Firewalls, Firewall)
xadmin.site.register(Upss, Ups)
xadmin.site.register(Broadbands, Broadband)
xadmin.site.register(Fibers, Fiber)
xadmin.site.register(APs, AP)
xadmin.site.register(ACs, AC)

xadmin.site.register(SwitchGroup, SwitchGroupAdmin)
xadmin.site.register(ServerGroup, ServerGroupAdmin)

xadmin.site.register(MaintainLog, MaintainLogAdmin)
xadmin.site.register(AccessRecord, AccessRecordAdmin)
